﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using CodeTier.DAL;

namespace CodeTier.BUL
{
    public class ChiTietPhieuDatHangBUS
    {
        public int MaCTPDH { get; set; }
        public int MaSP { get; set; }
        public int MaPDH { get; set; }
        public int SoLuong { get; set; }
        public int DonGia { get; set; }
        public string TinhTrang { get; set; }

        public ChiTietPhieuDatHangBUS()
        {

        }

        public bool update()
        {
            var result = (from o in CommonDAO.XDMH.ChiTietPDHs
                         where o.MaCTPDH == this.MaCTPDH
                          select o).SingleOrDefault() ;
            result.TinhTrang = this.TinhTrang;
            try
            {
                CommonDAO.XDMH.SubmitChanges();
                CommonDAO.XDMH.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, CommonDAO.XDMH.ChiTietPDHs);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static string selectByIdGetStatus(int mactpdh)
        {
            var result = (from o in CommonDAO.XDMH.ChiTietPDHs
                          where o.MaCTPDH == mactpdh
                          select o).Single();
            return result.TinhTrang;
        }


        public static List<ChiTietPhieuDatHangBUS> getAll(int MaPDH)
        {
            var results = from o in CommonDAO.XDMH.ChiTietPDHs
                          where o.MaPDH == MaPDH
                          select o;
            List<ChiTietPhieuDatHangBUS> lst_ct = new List<ChiTietPhieuDatHangBUS>();
            foreach (var result in results)
            {
                ChiTietPhieuDatHangBUS ct = new ChiTietPhieuDatHangBUS();
                ct.MaPDH = result.MaPDH;
                ct.MaCTPDH = result.MaCTPDH;
                ct.MaSP = result.MaSP;
                ct.SoLuong = result.SoLuong;
                ct.DonGia = Convert.ToInt32(result.DonGia);
                ct.TinhTrang = result.TinhTrang;
                lst_ct.Add(ct);
            }
            return lst_ct;
        }
    }
}
